// 引入
import CryptoJS from "crypto-js";

// 密钥
const AES_KEY = "fjtZr6Q3LlLGh3Nqy+IlWG=="; // 后端提供
// 初始化向量
const IV = "1234567887654321"; // 后端提供

//加密方法
export function encrypt(word) {
  let key = CryptoJS.enc.Utf8.parse(AES_KEY);
  let iv = CryptoJS.enc.Utf8.parse(IV);
  let srcs = CryptoJS.enc.Utf8.parse(word);

  let encrypted = CryptoJS.AES.encrypt(srcs, key, {
    iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
  });
  return encrypted.toString();
}

//解密方法
export function decrypt(word) {
  var key = CryptoJS.enc.Utf8.parse(AES_KEY);
  let iv = CryptoJS.enc.Utf8.parse(IV);

  var decrypt = CryptoJS.AES.decrypt(word, key, {
    iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
  });
  return decrypt.toString(CryptoJS.enc.Utf8).trim();
}
